package com.boot.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.boot.feign.ProductFeignClient;
import com.boot.order.pojo.Order;
import com.boot.product.pojo.Product;
import com.boot.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    ProductFeignClient productFeignClient;

    @Override
    @SentinelResource(value="create", blockHandler = "createFallBack", fallback = "createFallBack")
    public Order createOrder(Long productId, Long userId) {
        Product product = getProductFromRemoteByClient(productId);
        Order order = new Order();
        order.setId(1L);
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("jack");
        order.setAddress("chengdu");
        order.setProductList(List.of(product));
        return order;
    }

    @Override
    public Product getProductFromRemote(Long productId) {
        //获取商品服务所有机器IP和port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");

        ServiceInstance serviceInstance = instances.get(0);
        //拼接远程URL
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;

        //用restTemplate获取商品信息，封装一个对象出来
        return restTemplate.getForObject(url, Product.class);

    }

    @Override
    public Product getProductFromRemoteLoadBalance(Long productId) {
        //获取商品服务所有机器IP和port
        ServiceInstance serviceInstance = loadBalancerClient.choose("service-product");

        //拼接远程URL
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;
        System.out.println(url);
        //用restTemplate获取商品信息，封装一个对象出来
        return restTemplate.getForObject(url, Product.class);

    }

    @Override
    public Product getProductFromRemoteByAnnotation(Long productId) {
        String url = "http://service-product/product/" + productId;
        return restTemplate.getForObject(url, Product.class);
    }

    @Override
    public Product getProductFromRemoteByClient(Long productId) {
        return productFeignClient.getProduct(productId);
    }

    public Order createFallBack(Long productId, Long userId, BlockException e) {
        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息：" + e.getMessage());
        return order;
    }
}
